package org.classg.edu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.classg.edu.entity.Subject;
import org.classg.edu.listener.SubjectExcelListener;
import org.classg.edu.mapper.SubjectMapper;
import org.classg.edu.service.SubjectService;
import org.classg.edu.vo.SubjectOneVo;
import org.classg.edu.vo.SubjectTwoVo;
import org.classg.edu.vo.excel.SubjectData;
import org.classg.servicebase.exception.MyException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-05-14
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {

    // 添加课程分类
    @Override
    public void insertService(MultipartFile file, SubjectService subjectService) {
        try {
            InputStream is = file.getInputStream();
            EasyExcel.read(is, SubjectData.class, new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(20002, "添加课程分类失败");
        }
    }

    // 查看课程分类列表
    @Override
    public List<SubjectOneVo> queryAllSubject() {
        // 创建最终返回的list
        List<SubjectOneVo> finalItems = new ArrayList<>();
        // 1. 查询出所有一级分类
        List<Subject> oneSubjects = baseMapper.selectList(new LambdaQueryWrapper<Subject>().eq(Subject::getParentId, 0));
        // 2. 查询出所有二级分类
        List<Subject> twoSubjects = baseMapper.selectList(new LambdaQueryWrapper<Subject>().ne(Subject::getParentId, 0));
        // 3. 封装一级分类
        for (Subject oneSubject : oneSubjects) {
            SubjectOneVo subjectOneVo = new SubjectOneVo();
            /*subjectOneVo.setId(oneSubject.getId());
            subjectOneVo.setTitle(oneSubject.getTitle());*/
            // 使用BeanUtils来copy属性值 -> 将值1复制到值2
            BeanUtils.copyProperties(oneSubject, subjectOneVo);
            // 4. 封装二级分类
            // 创建一个临时集合用来存放二级分类
            List<SubjectTwoVo> temp = new ArrayList<>();
            for (Subject twoSubject : twoSubjects) {
                // 判断是否属于该一级分类的二级分类
                if (twoSubject.getParentId().equals(oneSubject.getId())) {
                    SubjectTwoVo subjectTwoVo = new SubjectTwoVo();
                    BeanUtils.copyProperties(twoSubject, subjectTwoVo);
                    subjectOneVo.setChildren(temp);
                    temp.add(subjectTwoVo);
                }
            }
            finalItems.add(subjectOneVo);
        }
        return finalItems;
    }

    // 查看课程分类列表（树形）
    @Override
    public List<Subject> queryAllSubject2() {
        // 查询所有
        List<Subject> subjects = baseMapper.selectList(null);
        List<Subject> finalList = buildSubject(subjects);
        return finalList;
    }

    // 查找顶层
    private List<Subject> buildSubject(List<Subject> subjects) {
        List<Subject> finalList = new ArrayList<>();
        for (Subject one : subjects) {
            if ("0".equals(one.getParentId())) {
                // 设置
                one.setLevel(1);
                finalList.add(queryChild(one, subjects));
            }
        }
        return finalList;
    }

    // 查找子类
    private Subject queryChild(Subject one, List<Subject> subjects) {
        //one.setChildren(new ArrayList<>());
        for (Subject subject : subjects) {
            if (one.getId().equals(subject.getParentId())) {
                if (one.getChildren() == null) {
                    one.setChildren(new ArrayList<>());
                }
                // 设置
                subject.setLevel(one.getLevel() + 1);
                one.getChildren().add(queryChild(subject, subjects));
            }
        }
        return one;
    }
}
